//页面初始化
$(function(){
	$('#tabs').myTabs({   
	      border:false,   
	      width:'1010px',
	      onSelect:function(title){
//			title=title.split('(')[0];
			if(title=='个人信息'){
				$("#myInfoDiv").show();
				$("#myRoleDiv").hide();
				$("#myPassWordDiv").hide();
			}else if(title=='我的角色'){
				$("#myInfoDiv").hide();
				$("#myRoleDiv").show();
				$("#myPassWordDiv").hide();
				findUserRole();
			}else if(title == '密码修改'){
				$("#myInfoDiv").hide();
				$("#myRoleDiv").hide();
				$("#myPassWordDiv").show();
			}
	      }
	    }); 
	
	$("#passwordRuleLink").bind('mouseover',function(){
		$("#passwordRule").show();
	}).bind('mouseout',function(){
		$("#passwordRule").hide();
	});
});

//区县联动
function changeCity(){
	var cityId = $("#city").val();
	if(cityId != ''){
		$.post(mainDomain+'/backend/findCountyVOsByCityId.action',{'cityId':cityId},function(rs){
			if(!rs.state){
				var ops = '<option value="">请选择</option>';
				$.each(rs, function(i, value){
					ops +='<option value="'+value.countyId+'">'+value.countyCname+'</option>';
				});
				$("#county").empty().append(ops);
			}
		});
	}else{
		var ops = '<option value="">请选择</option>';
		$("#county").empty().append(ops);
	}
}

//编辑用户信息或密码
function saveMyInfo(type){
	//获取页面参数
	var id = $("#backOperatorId").val();
	var staffNo = $("#staffNo").val();
	var department = $.trim($("#department").val());
	var mobile = $.trim($("#mobile").val());
	var password = null;
	var newPassword = null;
	var confirmNewPassword = null;
	if(type == 2){
		var password = $.trim($("#oldPassword").val());
		var newPassword = $.trim($("#newPassword").val());
		var confirmNewPassword = $.trim($("#confirmNewPassword").val());
		if(password == ''){
			$.messager.alert('提示',"旧密码不能为空！",'error');
			return false;
		}
		if(newPassword == ''){
			$.messager.alert('提示',"新密码不能为空！",'error');
			return false;
		}
		if(newPassword != confirmNewPassword){
			$.messager.alert('提示',"新密码必须与确认密码一致！",'error');
			return false;
		}
	}
	
	//参数
	var parm={
			'id':id,
			'staffNo':staffNo,
			'department':department,
			'mobile':mobile,
			'password':password,
			'newPassword':newPassword,
			'confirmNewPassword':confirmNewPassword
	};
	//编辑用户
	$.post(mainDomain+'/backend/updateMyBackendOperator.action',parm,function(result){
		if (result.success && result.state == 1) {
			if(type == 1){
				$.messager.alert('提示', '编辑成功！');
			}
			if(type ==2){
				$.messager.confirm('提示', '密码修改成功，请重新登录系统！', function(rs){
					$.post("/login/logout.action",null,function(rs){
						top.location='/login/index.action';
					},'json');
				});
			}
		}else{
			$.messager.alert('提示', result.message);
		}
	});
}

//获取用户角色
function findUserRole(){
	var url = mainDomain+'/backend/findUserRole.action';
	var backOperatorId = $("#backOperatorId").val();
	$.post(url,{'backOperatorId':backOperatorId},function(rs){
		var tabStr = '<tr><th>行号</th><th>角色名称</th><th>角色描述</th></tr>';
		if(rs){
			var row = 1;
			$.each(rs,function(i,v){
				if(null != this.roleVO ){
					tabStr += '<tr><td>'+row+'</td><td>'+this.roleVO.roleName+'</td><td>'+this.roleVO.remark+'</td></tr>';
					row++;
				}
			});
		}else{
			tabStr += '<tr><td colspan="3">无</td></tr>';
		}
		$("#myRoleTab").empty().html(tabStr);
	},'json');
}

